﻿/**
*
*
*
*
*  
*   
*	@class:		Odometer
*	@author:		andy hatch
*	@version:		1.0
*	@date:		2008-03-26
*/ 
import gs.TweenLite;
import mx.transitions.easing.*;


class com.hatch.effects.odometer.Odometer extends MovieClip{
	
	// ON STAGE
	
	
	// CLASS PROPERTIES
	var _segments:Array;
	var _words:Array;
	
	
	/* *********************************************************
	*  CONSTRUCTOR
	*  *********************************************************/
	
	function Odometer()
	{
		initialize();
	}


	private function initialize():Void
	{
		_segments = new Array();
		
		//define words
		_words = new Array("superannuation", "recontributed income", "testamentary trust", "debentures");
		//_words = new Array("superann uation"); //for testing
		
		
		buildOdometer();
		
		scrollToWord(1);
	} 



	/* *********************************************************
	*  PUBLIC METHODS
	*  *********************************************************/
	
	public function scrollToWord(wordNumber:Number):Void
	{
		var characters:Array = splitString(_words[wordNumber]);
		
		for(var i = 0; i < _segments.length; i++)
		{
			//if(characters[i] == undefined)
			//{
				//scroll to " " for empty character spaces;
				_segments[i].scrollToCharacter(" ");
			//}
			//else
			//{
				_segments[i].scrollToCharacter(characters[i]);
			//}	
		}
		
	}

	/* *********************************************************
	*  PRIVATE METHODS
	*  *********************************************************/
	
	private function getNextNumber(n:Number):Number
	{
		return (n == 9) ? 0 : ++n;
	}


	/**
	* getNextNumber
	*
	* @param
	*/
	private function getPrevNumber(n:Number):Number
	{
		return (n == 0) ? 9 : --n;
	}
	
	
	/**
	* buildOdometer
	*
	* @param
	*/
	
	
	
	private function buildOdometer()
	{
		trace("Odometer :: buildOdometer()");
		//for placing segments
		var xVal:Number = 0;
		var spacer:Number = 0;
		//attach segments
		for(var i = 0; i < getLongest(_words); i++)
		{
			var segmentName:String = "segment" + i;
			var segment = this.attachMovie("segment", segmentName, getNextHighestDepth());
			segment.initialize();
			
			segment._x = xVal;
			xVal += segment._width + spacer;
			
			_segments.push(segment);
		}
		
		setDefaultState();
	}
	
	private function setDefaultState():Void
	{
		
	}
	
	
	private function getLongest(words:Array):Number
	{
		var longestWord:Number = 0;
		for(var i = 0; i < words.length; i++)
		{
			if(words[i].length > longestWord)
			{
				longestWord = words[i].length;
			}
		}
		
		return longestWord;
	}
	
	/**
	* splitString
	*
	* @param
	*/
	private function splitString(n:String):Array
	{
		var s = String(n);
		var displayedChars = new Array();
		for(var i=0; i<s.length; i++)
		{
			displayedChars.push(s.charAt(i));
		}
		
		if(displayedChars < _segments.length)
		{
			var fillVal:Number = _segments.length - displayedChars.length;
			
			for (var i = 0; i < fillVal; i++)
			{
				displayedChars.push(" ");
			}
		}
		
		return displayedChars;
	}
	
	





	/* *********************************************************
	*  EVENT HANDLERS
	*  *********************************************************/






	/* *********************************************************
	*  GETTERS/SETTERS
	*  *********************************************************/    
	
};